using System;
using System.Collections.Generic;
using System.Linq;
using MTUnity.Utils;
using Script.Data;
using UnityEngine;

namespace Script.UI.Panel.OverviewWnd2
{
    public class SliderModule : MonoBehaviour
    {
        public Transform root;

        public List<SliderGroup> m_SliderGroups;
        
        public enum TypeName
        {
            维修,
            点检,
            巡检,
            保养
        }

        public void Awake()
        {
            m_SliderGroups = new List<SliderGroup>();
            for (int i = 0; i < root.childCount; i++)
            {
                m_SliderGroups.Add(root.GetChild(i).GetComponent<SliderGroup>());
            }
            // m_SliderGroups = new List<SliderGroup>(root.transform.GetComponentsInChildren<SliderGroup>());
        }

        public void RefreshSlider(MTJSONObject obj)
        {
            var list = obj.list;
            float max = float.MinValue;
            float min = float.MaxValue;
            Dictionary<int,List<MTJSONObject>> rlist = new  Dictionary<int,List<MTJSONObject>>();
            foreach (var mtjsonObject in list)
            {
                var c =mtjsonObject["num"].f;
                max = Mathf.Max(c, max);
                min = Mathf.Min(c, min);
                var id = mtjsonObject["loctionId"].i;
                if (!rlist.ContainsKey(id))
                {
                    rlist.Add(id , new List<MTJSONObject>());
                }
                rlist[id].Add( mtjsonObject);
            }

            int index = 0;
            foreach (var v in rlist)
            {
                m_SliderGroups[index].gameObject.SetActive(true); 
                m_SliderGroups[index].Refresh(v.Value, max, min);
                index += 1;
            }

            for (int i =index ; i < m_SliderGroups.Count; i++)
            {
                m_SliderGroups[i].gameObject.SetActive(false);
            }
        }
    }
}